package bit.lotterysystem.controller;

import bit.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import bit.lotterysystem.common.exception.ControllerException;
import bit.lotterysystem.common.pojo.CommonResult;
import bit.lotterysystem.common.utils.JacksonUtil;
import bit.lotterysystem.controller.param.ShortMessageLoginParam;
import bit.lotterysystem.controller.param.UserPasswordLoginParam;
import bit.lotterysystem.controller.param.UserRegisterParam;
import bit.lotterysystem.controller.result.BaseUserInfoResult;
import bit.lotterysystem.controller.result.UserRegisterResult;

import bit.lotterysystem.controller.result.UserLoginResult;
import bit.lotterysystem.service.UserService;
import bit.lotterysystem.service.VerificationCodeService;
import bit.lotterysystem.service.dto.UserDTO;
import bit.lotterysystem.service.dto.UserLoginDTO;
import bit.lotterysystem.service.dto.UserRegisterDTO;
import bit.lotterysystem.service.enums.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;




@RestController
public class UserController {

    private static final Logger logger=LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @Autowired
    private VerificationCodeService verificationCodeService;

    /**
     * 注册
     */
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param){
        //日志打印
         logger.info("userRegister UserRegisterParam:{}", JacksonUtil.writeValueAsString(param));

         //校验注册信息
        UserRegisterDTO userRegisterDTO = userService.register(param);

        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }


    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        if(userLoginDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult result=new UserLoginResult();
        result.setToken(userLoginDTO.getToken());
        result.setIdentity(userLoginDTO.getIdentity().name());
        return result;
    }

    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        if(userRegisterDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        UserRegisterResult result=new UserRegisterResult();
        result.setUserId(userRegisterDTO.getUserId());
        return result;
    }

    private List<BaseUserInfoResult> convertToList(List<UserDTO>userDTOList){
        if(userDTOList==null){
            return Arrays.asList();
        }
        return userDTOList.stream().map(userDTO -> {
            BaseUserInfoResult result=new BaseUserInfoResult();
            result.setUserId(userDTO.getId());
            result.setUserName(userDTO.getUserName());
            result.setIdentity(userDTO.getIdentity());
            return result;
        }) .collect(Collectors.toList());



    }


    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode( String phoneNumber){
        logger.info("sendVerificationCode phoneNumber:{}", phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(true);
    }

    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(@RequestBody @Validated ShortMessageLoginParam param){
        logger.info("shortMessageLogin param:{}", JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfoList(String identity){
        logger.info("findUserBaseInfoList identity:{}", identity);
        List<UserDTO>userDTOList=userService.findUserInfo(UserIdentityEnum.forName(identity));
        return CommonResult.success(convertToList(userDTOList));
    }


    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(@RequestBody @Validated UserPasswordLoginParam param){
        logger.info("userPasswordLogin param:{}", JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    /**
     * 测试的人员数据
     * 管理员
     * 姓名：admin
     * 邮箱：admin@qq.com
     * 电话：13729493131
     * 密码：123456
     */


}
